package br.com.srv.service;

import java.util.List;
import java.util.Map;

import HTTPClient.HTTPConnection;
import br.com.srv.util.ArenaHelper;

public class CadastroService extends ArenaBaseService {
	/**
	 * Construtor que cria um service sem um HTTPConnection.
	 */
	public CadastroService() {
		this(null);
	}
	/**
	 * Construtor que cria um service com um HTTPConnection.
	 * 
	 * @param dataSource
	 */
	public CadastroService(HTTPConnection connection) {
		super(connection);
	}

	public String savePoint(Map params) throws Exception {
		Boolean novoPonto = (Boolean) params.get("novoPonto");
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER + "\n");
		urlString.append("<POINT>\n");
		urlString.append("	<UPDATE_POINT_ROUTES>0</UPDATE_POINT_ROUTES>\n");
		if (novoPonto) {
			urlString.append("	<OLD_ID>0</OLD_ID>\n");
			urlString.append("	<ID>0</ID>\n");
			urlString.append("	<X>" + params.get("x") + "</X>\n");
			urlString.append("	<Y>" + params.get("y") + "</Y>\n");
			urlString.append("	<COD>" + params.get("clientId") + "_" + 
			                    params.get("pontoId") + "_" + 
			                    params.get("descricao")
			                    	.toString().replace(" ", "")
			                    	.substring(0, 3).toUpperCase() +
							"</COD>\n");
			urlString.append("	<DIST_HOR>0</DIST_HOR>\n");
			urlString.append("	<DIST_VER>0</DIST_VER>\n");
			urlString.append("	<TYPE_ID>1</TYPE_ID>\n");
			urlString.append("	<ICON_ID>0</ICON_ID>\n");
		} else {
			Map ponto = findPoint(params);
			urlString.append("	<OLD_ID>" + ponto.get("ID") + "</OLD_ID>\n");
			urlString.append("	<ID>" + ponto.get("ID") + "</ID>\n");
			urlString.append("	<X>" + ponto.get("X") + "</X>\n");
			urlString.append("	<Y>" + ponto.get("Y") + "</Y>\n");
			urlString.append("	<COD>" + ponto.get("COD") + "</COD>\n");
			urlString.append("	<DIST_HOR>" + ponto.get("DIST_HOR") + "0</DIST_HOR>\n");
			urlString.append("	<DIST_VER>" + ponto.get("DIST_VER") + "</DIST_VER>\n");
			urlString.append("	<TYPE_ID>" + ponto.get("TYPE_ID") + "</TYPE_ID>\n");
			urlString.append("	<ICON_ID>" + ponto.get("ICON_ID") + "</ICON_ID>\n");
		}
		urlString.append("	<CLIENT_ID>" + params.get("clientId") + "</CLIENT_ID>\n");
		urlString.append("	<DESC>" + params.get("descricao") + "</DESC>\n");
		if (params.get("endereco") != null && !params.get("endereco").equals("")) { 
			urlString.append("	<ADDRESS>" + params.get("endereco") + "</ADDRESS>\n");
		}
		urlString.append("	<SHARE>1</SHARE>\n");
		urlString.append("</POINT>\n");

		String xml = queryArenaMethod(ArenaHelper.WEB_SERVICE_POINT, urlString);

		return xml;
	}

	public String deletePoint(Map params) throws Exception {
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER + "\n");
		urlString.append("<ITEMS>\n");
		urlString.append("	<ITEM>" + params.get("pontoId") + "</ITEM>\n");
		urlString.append("</ITEMS>\n");

		String xml = queryArenaMethod(ArenaHelper.WEB_SERVICE_POINT, urlString);

		return xml;
	}
	
	private Map findPoint(Map params) throws Exception {
		StringBuffer urlString = new StringBuffer(ArenaHelper.XML_READER + "\n");
		urlString.append("<FILTER>\n");
		urlString.append("	<IDS><ID>" + params.get("pontoId") + "</ID></IDS>\n");
		urlString.append("	<CLIENT_ID>" + params.get("clientId") + "</CLIENT_ID>\n");
		urlString.append("	<SHOW_FIELDS>\n");
		urlString.append("		<ID>1</ID>\n");
		urlString.append("		<X>1</X>\n");
		urlString.append("		<Y>1</Y>\n");
		urlString.append("		<COD>1</COD>\n");
		urlString.append("		<DESC>1</DESC>\n");
		urlString.append("		<CLIENT_ID>1</CLIENT_ID>\n");
		urlString.append("		<DIST_HOR>1</DIST_HOR>\n");
		urlString.append("		<DIST_VER>1</DIST_VER>\n");
		urlString.append("		<TYPE_ID>1</TYPE_ID>\n");
		urlString.append("		<ICON_ID>1</ICON_ID>\n");
		urlString.append("	</SHOW_FIELDS>\n");
		urlString.append("</FILTER>\n");
		String xml = queryArenaMethod(ArenaHelper.WEB_SERVICE_SHOWPOINTS, urlString);
		List pontos = parseList(xml, "/POINTS", "POINT");
		return (pontos.isEmpty()) ? null : (Map) pontos.get(0);
	}
}
